package cn.com.smartdevices.bracelet.chart.base;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

import cn.com.smartdevices.bracelet.chart.data.ChartDataLoader;

public abstract class BaseChartView<T extends BaseChart<? extends Item>> extends View {
    private static final String f3846a = "Chart.BaseView";
    protected T f3847e;
    protected ChartDataLoader f3848f;
    protected boolean f3849g;
    protected ChartScroller f3850h;
    protected boolean f3851i;
    protected int f3852j;
    protected boolean f3853k;
    protected boolean f3854l;
    protected Interpolator f3855m;
    protected long f3856n;
    protected float f3857o;
    protected Animator f3858p;

    public BaseChartView(Context context) {
        this(context, null);
    }

    public BaseChartView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public BaseChartView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.f3853k = true;
        this.f3857o = 1.0f;
        this.f3855m = new LinearInterpolator();
        this.f3856n = 500;
    }

    class C0753h implements ValueAnimator.AnimatorUpdateListener {
        final /* synthetic */ BaseChartView f3957a;

        C0753h(BaseChartView baseChartView) {
            this.f3957a = baseChartView;
        }

        @SuppressLint({"NewApi"})
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            this.f3957a.f3857o = valueAnimator.getAnimatedFraction();
            this.f3957a.postInvalidateOnAnimation();
        }
    }

    @SuppressLint({"NewApi"})
    private void baseChartViewRefresh(float f, boolean z) { // m8392a
        if (z) {
            animRefresh();
            if (!this.f3858p.isStarted()) {
                this.f3858p.start();
                return;
            }
            return;
        }
        this.f3857o = f;
        //TBD DEBUG
//        Log.e("Chart.BaseView", GuliLog.stackTracePrint() + "start invalidate");

        postInvalidateOnAnimation();
    }

    public Animator animRefresh() { //m8393a
        if (this.f3858p == null) {
            this.f3858p = animRefresh(this.f3856n);
        }
        return this.f3858p;
    }

    public void setDataLoader(ChartDataLoader c0746d) // m8394a
    {
        this.f3848f = c0746d;
    }

    public void refresh(boolean z) // m8395a
    {
        baseChartViewRefresh(z ? 0.0f : 1.0f, z);
    }

    public void refresh() {  //m8396c
        baseChartViewRefresh(1.0f, false);
    }

    public void setScrollable(boolean z) { // m8397c
        this.f3854l = z;
    }

    public Animator animRefresh(long j) { //m8398d
        ValueAnimator ofFloat = ValueAnimator.ofFloat(new float[]{0.0f});
        ofFloat.setDuration(j);
        ofFloat.setInterpolator(this.f3855m);
        ofFloat.addUpdateListener(new C0753h(this));
        return ofFloat;
    }

    public void m8399d(boolean z) {
        this.f3849g = z;
    }

    public void refresh(float f)  // m8400e
    {
        baseChartViewRefresh(f, false);
    }

    public void attach() { // m8401h
        this.f3853k = true;
    }

    public void detach() {  // m8402i
        this.f3853k = false;
    }

    public boolean isAttached() { // m8403j
        return this.f3853k;
    }

    public boolean m8404k() {
        return this.f3854l;
    }

    public long m8405l() {
        return this.f3856n;
    }

    public ChartDataLoader getDataLoader() { //m8406m
        return this.f3848f;
    }

    protected void onDraw(Canvas canvas) {
        this.f3847e.draw(canvas, this.f3857o);
    }

    protected void onSizeChanged(int i, int i2, int i3, int i4) {
        Rect rect = new Rect();
        rect.left = getPaddingLeft();
        rect.top = getPaddingTop();
        rect.right = i - getPaddingRight();
        rect.bottom = i2 - getPaddingBottom();
        this.f3847e.setRect(rect);
    }
}
